package com.mayi.zds.web;

import com.mayi.zds.entity.Business;
import com.mayi.zds.entity.User;
import com.mayi.zds.exception.BusinessAccountPasswordErrorException;
import com.mayi.zds.exception.BusinessStatusErrorException;
import com.mayi.zds.exception.ServiceException;
import com.mayi.zds.service.BusinessService;
import com.mayi.zds.service.GoodsService;
import com.mayi.zds.utils.ZdsUtils;
import com.mayi.zds.vo.CartsVO;
import com.mayi.zds.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.BindingResult;
import javax.validation.Valid;

@Controller
@SessionAttributes("business")
@RequestMapping("business")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    private GoodsService goodsService;
    @PostMapping("register")
    public String register(@ModelAttribute("business")@Valid Business business, BindingResult result, Model model){
    if(result.hasErrors()){
        model.addAttribute("errors",result.getFieldErrors());
        return "register";
    }
    try {
        businessService.add(business);
        model.addAttribute("message","注册成功请等待审核");
        return "register_success";
    }catch (ServiceException e){
        model.addAttribute("message","用户名已被占用");
        return "register";
    }catch (DataAccessException e){
        model.addAttribute("message","未知错误");
        return  "register";
    }

    }
    @PostMapping("login")
    public String login(String account,String Password,Model model){
        try{businessService.login(account,Password);
        model.addAttribute("business",businessService.login(account,Password));

        }catch (
                ServiceException e
        ){if (e instanceof BusinessAccountPasswordErrorException){
            model.addAttribute("message","登录失败，用户名密码不匹配");
            return "login";
        }else if(e instanceof BusinessStatusErrorException){
            model.addAttribute("message","该商户正在审核或状态异常请联系管理员");
            return "login";
        }else{
            model.addAttribute("message","未知错误··");
            return "login";
        }

        }
        return "business_center";
    }
    /**
     * @Author 刘宇涛
     * @Description  跳转到商铺详情页面
     * @Date 14:52 2019/12/26
     * @Param [businessId, model]   商铺id，用于存商铺id并发往页面，页面发送ajax取数据
     * @return java.lang.String 返回商品列表页面
     **/
    @GetMapping("/view")
    public String view(String businessId, Model model){
        model.addAttribute("businessId",businessId);
        return "shop_detail";
    }
    /**
     * @Author 刘宇涛
     * @Description  通过商铺id查对应数据（主要包含新建一个cartVO（购物车信息，包含对应商品（id，数量），本购物车id
     * （没什么用）），商铺信息，标签集合（标签下的商品集合））
     * @Date 15:56 2019/12/26
     * @Param [businessId, user]
     * @return com.mayi.zds.vo.ResponseVO 主要包含新建一个cartVO（购物车信息，包含对应商品（id，数量），本购物车id
     * （没什么用）），商铺信息，标签集合（标签下的商品集合））
     **/
    @GetMapping("getData/{businessId}")
    @ResponseBody
    public ResponseVO getBusinessData(@PathVariable("businessId") String businessId,
                                      @SessionAttribute("user") User user){
        Business business=businessService.getById(businessId);
        CartsVO cart=new CartsVO();
        cart.setId(ZdsUtils.produceId());
        cart.setUserId(user.getId());
        cart.setBusiness(business);
        ResponseVO responseVO=new ResponseVO();
        responseVO.setCode("200");
        responseVO.setMsg("查商铺数据");
        responseVO.setData(cart);
        return responseVO;
    }

    /**
     * 删除商品
     * @param goodsId
     * @return
     */
    @GetMapping("goodsdel/{goodsId}")
    @ResponseBody
    public ResponseVO deleteGoods(@PathVariable("goodsId") String goodsId){
        goodsService.delete(goodsId);
        ResponseVO responseVO=new ResponseVO();
        responseVO.setMsg("删除成功");
        responseVO.setCode("200");
        return responseVO;
    }

    /**
     * 下架商品
     * @param goodsId
     * @return
     */
    @GetMapping("goodsdown/{goodsId}")
    @ResponseBody
    public ResponseVO downGoods(@PathVariable("goodsId") String goodsId){
        goodsService.down(goodsId);
        ResponseVO responseVO=new ResponseVO();
        responseVO.setMsg("下架成功");
        responseVO.setCode("200");
        return responseVO;
    }

    }

